package com.shanzhu.volunteer.interfaces.controller.user;


import com.shanzhu.volunteer.common.component.page.R;
import com.shanzhu.volunteer.common.exception.ServiceException;
import com.shanzhu.volunteer.common.exception.file.FileSizeLimitExceededException;
import com.shanzhu.volunteer.common.utils.SecurityUtil;
import com.shanzhu.volunteer.modules.volunteer.service.CampusFileService;
import com.shanzhu.volunteer.modules.volunteer.service.ContentLoveService;
import com.shanzhu.volunteer.modules.volunteer.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户操作api
 *
 * 
 */
@RestController
@RequestMapping("/campus")
//@ApiResource(name = "用户操作api", appCode = "campus", resBizType = ResBizTypeEnum.BUSINESS)
public class UserActionController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private ContentLoveService contentLoveService;
    @Autowired
    private CampusFileService campusFileService;


    /**
     * 点赞
     */
    
    @GetMapping(value = "/zan/{contentId}", name = "点赞")
    public R zanContent(@PathVariable Long contentId) {
        Long userId = SecurityUtil.getUserId();

        return R.ok(contentLoveService.zanContent(userId, contentId));
    }

    /**
     * 图片文件上传
     */
    
    @PostMapping(value = "/imageUpload", name = "图片文件上传")
    public R imageUpload(MultipartFile file) {
        if (!file.isEmpty()) {
            return R.ok(campusFileService.fileUploadImage(file));
        }
        return R.error("上传异常");
    }

    /**
     * 视频文件上传
     */
    
    @PostMapping(value = "/videoUpload", name = "视频文件上传")
    public R videoUpload(MultipartFile file) {
        if (!file.isEmpty()) {

            return R.ok(campusFileService.fileUploadVideo(file));
        }
        return R.error("上传异常");
    }

    /**
     * 删除文件
     */

    @DeleteMapping(value = "/{fileId}", name = "删除文件")
    public R deleteFile(@PathVariable Long fileId,
                        @RequestParam(required = false) String fileName,
                        @RequestParam Integer type) {
        try {
            campusFileService.deleteFile(fileId, fileName, type);
            //顺便删除信息墙没分配的文件
//            campusFileService.removeCampusFile();
            return R.ok().put("msg", "文件删除成功");
        } catch (Exception e) {
            throw new ServiceException("删除文件失败", 500);
        }
    }
}
